#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define mes(x) memset(x,0,sizeof(x))
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout)

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}
const int MAXN = 1e5+10;

int n,top,stk[MAXN];
ll a[MAXN],s[MAXN],f[MAXN];

int main (){
	n = in;for(int i = 1;i <= n;i++) a[i] = in,s[i] = s[i-1] + a[i]; s[n+1] = s[n];
	for(int i = 1;i <= n+1;i++){
		while(a[i] < a[stk[top]]) f[stk[top]] += s[i-1] - s[stk[top]],top--;
		f[i] += s[i] - s[stk[top]]; stk[++top] = i;
	}ll ans = 0;
	for(int i = 1;i <= n;i++) ans = max(ans,f[i] * a[i]);
	printf("%lld\n",ans);
	return 0;
}

